package com.zhuiyun.project.api.record.service.impl;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.zhuiyun.project.api.collectVideo.entity.Favorites;
import com.zhuiyun.project.api.record.entity.Record;
import com.zhuiyun.project.api.record.mapper.RecordMapper;
import com.zhuiyun.project.api.record.model.RecordModel;
import com.zhuiyun.project.api.record.service.RecordService;
import com.zhuiyun.project.api.topicmanage.entity.Topic;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName RecordServiceImpl
 * @Description TODO
 * @Author XC8
 * @Date 2024/2/26 14:35
 **/
@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordMapper recordMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static final String RECORD_KEY="RECORD_KEY";

    /**
    * @Author XC8
    * @Description 观看记录
    * @Date 2024/2/26 14:38
    * @Return com.zhuiyun.project.common.response.CommonResult

    */
    @Override
    public CommonResult getRecordById(Integer userId, String teDay) throws CommonException {

//        String jsonStr = stringRedisTemplate.opsForValue().get(RECORD_KEY);

        List<Record> recordList= recordMapper.getRecordById(userId,getToday());
//        if(StrUtil.isBlank(jsonStr)){//缓存取到是空的
//            recordList = recordMapper.getRecordById(userId,getToday());
//            //一个月清一次
//            stringRedisTemplate.opsForValue().set(RECORD_KEY, JSONUtil.toJsonStr(recordList),10L, TimeUnit.HOURS);
//        }else {
//            recordList = JSONUtil.toBean(jsonStr, new TypeReference<List<Record>>() {
//            }, true);//true JSON转化出错就忽略掉
//        }
//        recordMapper.getRecordById(userId)

        return CommonResult.ok(recordList);
    }

    /**
    * @Author XC8
    * @Description 根据id删除
    * @Date 2024/2/26 15:35
    * @Return com.zhuiyun.project.common.response.CommonResult
    */
    @Override
    public CommonResult recordDelete(Integer rId) {
        if(rId!=0){
            int i=recordMapper.recordDelete(rId);
            if(i>0){
                flushRedis(RECORD_KEY);
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else {
                return CommonResult.error();
            }
        }else {
            return CommonResult.error(EmErrorCode.ID_NULL);
        }
    }

    /**
    * @Author XC8
    * @Description 生成观看记录
    * @Date 2024/3/8 9:24
    * @Return com.zhuiyun.project.common.response.CommonResult

    */
    @Override
    public CommonResult addRecord(RecordModel recordModel) throws CommonException {
        if (ValidatorUtil.ValidationObject(recordModel)) {
            Record record = new Record();
            BeanUtils.copy(recordModel, record);
            int i = recordMapper.addRecord(record);
            if (i > 0) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            } else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        } else {
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /**
    * @Author XC8
    * @Description 刷新radis
    * @Date 2024/3/5 9:01
    * @Return void

    */
    private void flushRedis(String key){
        stringRedisTemplate.delete(key);
    }

    /**
    * @Author XC8
    * @Description 查询一个月前的日期
    * @Date 2024/3/5 9:01
    * @Return java.lang.String
    */
    private String getToday(){
        LocalDate now = LocalDate.now();
        LocalDate oneMonthAgo = now.minusMonths(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = oneMonthAgo.format(formatter);
        System.out.println(formattedDate);
        return formattedDate;
    }
}
